import 'package:flutter/material.dart';

///
/// (1) ElevatedButton
/// (2) ScaffoldMessenger.of(context) 应该在 await 之前获取
/// (3) Navigator.pop(context, '小蛮腰小姐姐, 15985458974') 可以传递参数
/// (4) async await
///
class DemoNavigator3 extends StatelessWidget {
  const DemoNavigator3({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: '跳转页面返回数据',
      home: FirstPage(),
    );
  }
}

///
/// 第一页
///
class FirstPage extends StatelessWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('找小姐姐要电话')),
      body: const Center(
        child: RouteButton(),
      ),
    );
  }
}

///
/// 按钮组件
///
class RouteButton extends StatelessWidget {
  const RouteButton({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(style: raisedButtonStyle,
        onPressed: (){_navigateToGirl(context);},
        child: const Text('去找小姐姐'));
  }

  _navigateToGirl(BuildContext context) async {
    final messenger = ScaffoldMessenger.of(context);
    final result = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context)=>const GirlWidget())
    );

    // (1) 下面的调用方式已经被官方弃用, 应该使用 `ScaffoldMessenger.of(context)`
    // Scaffold.of(context).showSnackBar()
    //
    // (2) 在异步方法中使用`ScaffoldMessenger.of(context)`要当心,
    // 应该将 `ScaffoldMessenger.of(context)` 放在 await 之前调用!
    // 参考 : https://zhuanlan.zhihu.com/p/545595079
    //
    // Do not use BuildContexts across async gaps.
    // https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html
    //
    // ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(result)));
    messenger.showSnackBar(SnackBar(content: Text(result)));
  }
}

///
/// 小姐姐组件
///
class GirlWidget extends StatelessWidget {
  const GirlWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('我是小姐姐'),),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              style: raisedButtonStyle,
              child: const Text('大长腿小姐姐'),
              onPressed: () {
                Navigator.pop(context, '大长腿小姐姐, 15985456589');
              },
            ),
            ElevatedButton(
              style: raisedButtonStyle,
              child: const Text('小蛮腰小姐姐'),
              onPressed: () {
                Navigator.pop(context, '小蛮腰小姐姐, 15985458974');
              },
            )
          ],
        ),
      ),
    );
  }
}

///
/// button 样式
/// New Buttons and Button Themes : https://flutter.cn/docs/release/breaking-changes/buttons
///
final ButtonStyle raisedButtonStyle = ElevatedButton.styleFrom(
  onPrimary: Colors.black87,
  primary: Colors.grey[300],
  minimumSize: const Size(88, 36),
  padding: const EdgeInsets.symmetric(horizontal: 16),
  shape: const RoundedRectangleBorder(
    borderRadius: BorderRadius.all(Radius.circular(2)),
  ),
);

